﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
//using MySql.Web;
using System.Configuration;
using MySQL.Db;
using System.Collections.ObjectModel;
using Gollie.Ajax;
using System.Diagnostics;
using System.IO;
using System.Web.Script.Serialization;

using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;

using DrawingImage = System.Drawing.Image;

namespace Gollie.Controls
{
	public partial class Home : BaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			//Stopwatch s = Stopwatch.StartNew();
			//for (int i = 0; i < 1000; i++)
			//{
			//	logger.Info("Dit is een test");
			//}
			//s.Stop();
			//int a = 0;
			//var a = new WebMethods().InsertPendingPayment("{\"tokenUser\":46,\"token\":\"wnVBklLlEeSl/WQxUCUxeA==\",\"samount\":\"1250\"}");
			//var a = Database.Queries.GetAjaxPaymentInfo(47, 46);
			//string arg = "{\"tokenUser\":17,\"token\":\"66F+q1VJEeSJXvgPQRiuyw==\",\"ticket\":\"9DNTPt0b0ggFAAAAEQAAAE4wFLL6w0/iq001yA2O7TJ2ycAHGKIxe4Z0dmDR6Cyr\",\"suserId\":\"I3NZCT2zlhNDaTkNg6Zecw%3d%3d\",\"samount\":400,\"scredit\":10600,\"pw\":\"6775\",\"email\":\"1\"}";
			//Gollie.Ajax.PayArg obj;

			
			//Stopwatch s = Stopwatch.StartNew();
			//for (int i = 0; i < 100; i++)
			//{
			//	JavaScriptSerializer serializer = new JavaScriptSerializer();
			//	serializer.MaxJsonLength = 4 * 1024 * 1024;
			//	obj = serializer.Deserialize<Gollie.Ajax.PayArg>(arg);
			//}
			//s.Stop();
			//int a = 0;

			//this.CreateQRCode();
			//string encoded = Server.UrlEncode(Convert.ToBase64String(Hash.Encrypt("00000004")));
			//uint orig = Hash.ConvertEncryptedUserId(encoded);

			//uint orig = Hash.Decrypt("I3NZCT2zlhNDaTkN%2f6Zbcw%3d%3d");
			//orig = Hash.ConvertEncryptedUserId("6xmckMqzR49N0KHEP%2bQ68nVieZhpmVdHxw%3d%3d");
			//Stopwatch s = new Stopwatch();
			//string encoded = Server.UrlEncode(Convert.ToBase64String(Hash.Encrypt("00000046")));
			//s.Start();
			//var paymentInfo = PaymentInfo.InitPayment(encoded, "00000047", null);
			//s.Stop();
			//int a = 0;
//			Gollie.Payment.EasyIdeal.IDealBanks banks = new Payment.EasyIdeal.IDealBanks();

			//byte[] key = new byte[] { 31, 127, 101, 5, 1, 191, 170 };

			//string s = System.Convert.ToBase64String(key);

		//	MySqlCommand command = new MySqlCommand("SELECT id, password, pincode FROM user");
		//	string connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
		//	MySqlConnection connection = new MySqlConnection(connectionString);
		//	connection.Open();
		//	command.Connection = connection;

		//	MySqlDataReader reader = command.ExecuteReader();

		//	Collection<MySqlCommand> updates = new Collection<MySqlCommand>();

		//	object[] row = new object[3];
		//	while (reader.Read())
		//	{
		//		reader.GetValues(row);
		//		GetUpdateCommands(row, updates);
		//	}

		//	reader.Close();

		//	foreach (MySqlCommand update in updates)
		//	{
		//		update.Connection = connection;
		//		int rows = update.ExecuteNonQuery();
		//	}


		//	connection.Close();
		//}

		//private void GetUpdateCommands(object[] row, Collection<MySqlCommand> commands)
		//{
		//	uint id = (uint)row[0];
		//	string oldPw = (string)row[1];
		//	string oldPin = row[2] is DBNull ? null : (string)row[2];

		//	string newPw = GetHash(oldPw);
		//	string newPin = oldPin == null ? null : GetHash(oldPin);

		//	string command = "UPDATE user SET password = '" + newPw + "' WHERE id = " + id.ToString();
		//	commands.Add(new MySqlCommand(command));

		//	if (newPin != null)
		//	{
		//		string pinCommand = "UPDATE user SET pincode = '" + newPin + "' WHERE id = " + id.ToString();
		//		commands.Add(new MySqlCommand(pinCommand));
		//	}
		}

		//private string GetHash(string hexHash)
		//{
		//	byte[] bin = new byte[64];

		//	int bIndex = 0;
		//	for (int i=0; i<hexHash.Length; i+=2)
		//	{
		//		byte b = Convert.ToByte(hexHash.Substring(i, 2), 16);
		//		bin[bIndex++] = b;
		//	}

		//	return Convert.ToBase64String(bin);
		//}
		//private void CreateQRCode()
		//{
		//	string text = "https://www.gollie.nl";

		//	QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H);
		//	QrCode code = encoder.Encode(text);
		//	GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

		//	int logoMargin = 6;
		//	int qrBorderWidth = 10;
		//	int extraHeight = 2 * logoMargin - qrBorderWidth;

		//	MemoryStream memStream = new MemoryStream();

		//	renderer.WriteToStream(code.Matrix, ImageFormat.Png, memStream);
		//	DrawingImage img = DrawingImage.FromStream(memStream);

		//	DrawingImage logo = DrawingImage.FromFile(Server.MapPath("/Images/LogoSmall.png"));

		//	Bitmap newImage = new Bitmap(img.Width, img.Height + logo.Height + extraHeight);
		//	Graphics g = Graphics.FromImage(newImage);
		//	g.DrawImage(img, 0, 0);

		//	RectangleF drawRect = new RectangleF(0, img.Height, img.Width, logo.Height + extraHeight);
		//	g.FillRectangle(Brushes.White, drawRect);

		//	g.DrawImage(logo, (img.Width - logo.Width) / 2, img.Height + extraHeight / 2);

		//	newImage.Save("d:\\tmp\\Gollie_0000000001.png", ImageFormat.Png);

		//	g.Dispose();
		//	img.Dispose();
		//	newImage.Dispose();
		//	logo.Dispose();
		//}
	}
}